package com.kbbywyclient.ui.fragment.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kbbywyclient.app.App;
import com.kbbywyclient.di.AppComponent;
import com.umeng.analytics.MobclickAgent;

import javax.inject.Inject;

import butterknife.ButterKnife;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by FHW
 * on 2017/3/28
 */

public abstract class BaseFragment extends Fragment {
    private CompositeSubscription mCompositeSubscription;
    @Inject
    ProgressDialog progressDialog;//进度框

    protected abstract int provideContentViewId();

    protected abstract void initView();

    protected abstract void initData();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(provideContentViewId(), container, false);
        ButterKnife.bind(getActivity(), rootView);
        initView();
        initAppComponent(App.getInstance().getAppComponent());
        initData();
        return rootView;

    }


    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(getContext());
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(getContext());
    }

    /**
     * add subscription, unsubscribe it when activity ondestory
     *
     * @param subscription
     */
    public void addSubscription(Subscription subscription) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        this.mCompositeSubscription.add(subscription);
    }

    protected abstract void initAppComponent(AppComponent appComponent);

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mCompositeSubscription != null) {
            mCompositeSubscription.unsubscribe();
        }
    }


    /**
     * 显示进度框
     *
     * @param title
     * @param content
     */
//    public void showProgress(String title, String content) {
//        if (progressDialog != null)
//            if (progressDialog.isShowing())
//                progressDialog.dismiss();
//        progressDialog.setTitle(title);
//        progressDialog.setMessage(content);
//        progressDialog.show();
//    }

    /**
     * 隐藏进度框
     */
//    public void dismissProgress() {
//        if (progressDialog != null)
//            progressDialog.dismiss();
//    }
}
